import Vue from 'vue';
import tpl from './tpl.html';

import WebBox from '../../component/WebBox/WebBox';
import Loading from '../../../grsc-base/component/Loading.vue';
import service from './service'
import lib from '../../common/lib';
import SelectK from '../../component/SelectK.vue';
import filter from '../../../grsc-base/util/filter';

import lang from './lang.js';

const yy = {
    cn: ['移动','联通','电信'],
    hk: ['移動','聯通','電信']
};

export default Vue.extend({
    template: tpl,
    components: {
        WebBox,
        Loading,
        SelectK
    },
    data() {
        return {
            form:{
                cardNo: '',
                password: ''
            },
            isRequesting: false, // 按钮点击  是否请求中
            isLoading: false,
            isLogin: false,   // 是否登陸  true已经登陆  false没登陆

            // selectTitMoney: '金额',
            defaultSeledMoneyVal: 10,
            selectOptionsMoney: [
                {name: '10元', value: 10},
                {name: '20元', value: 20},
                {name: '30元', value: 30},
                {name: '50元', value: 50},
                {name: '100元', value: 100},
                {name: '300元', value: 300},
                {name: '500元', value: 500}
            ],


            // selectTitYY: '运营商',
            defaultSeledYYVal: 0,
            selectOptionsYY: [
                {name: '', value: 0},
                {name: '', value: 1},
                {name: '', value: 2}
            ],

            cardNoErr: '',
            passwordErr: '',

            uscNum: ''
        }
    },
    created() {
        this.selectOptionsYY.forEach((item,index)=>{
            item.name = yy[this.$i18n.locale][index];
        });
        this.isLogin = lib.isLogin();
    },
    mounted() {
        this.getUsc();
    },
    methods: {
        getUsc(){
            let uscNum = (this.defaultSeledMoneyVal * 0.975) / 6.69;
            this.uscNum = filter.floorFix(''+uscNum, 3);
        },
        // // 充值点击
        getPresned(){
            if(!this.isLogin){
                // 未登陆  需要登陆
                setTimeout(()=>{
                    let fromPath = window.encodeURIComponent(window.location.href);
                    window.location.href = './login.html?from=' + fromPath;
                },3000)

                window.error(`未登錄，3秒後自動跳轉登錄頁面`);
                return;
            }

            if(!this.form.cardNo){
                console.log(this.$i18n.locale)
                this.cardNoErr = lang[this.$i18n.locale].cardNoErr;//'请输入正确的充值卡卡号';
            }

            if(!this.form.password){
                this.passwordErr = lang[this.$i18n.locale].passwordErr; //'请输入正确的充值卡密码';
                return;
            }
            this.isRequesting = true;
            service.getPresned({
                cardTypeCombine: this.defaultSeledYYVal,
                payMoney: this.defaultSeledMoneyVal,
                cardNum: this.form.cardNo,
                cardPwd: this.form.password
            }).done((req)=> {
                this.isGetCheckCode = req.code
                if(req.data.code == 200){
                    this.form.cardNo = '';
                    this.form.password = '';
                    window.success(req.msg);
                } else if(req.data.code == 404){
                    window.error(this.$t('reqNoneErr'));
                }else{
                    window.error(this.$t('reqError'));
                }
            }).always(() => {
                this.isRequesting = false;
                this.clearErrInfo();
            });


           
            
        },
        clearErrInfo(){
            this.cardNoErr = '';
            this.passwordErr = '';
        },
        clearpwdErr(){
            this.passwordErr = '';
        },
        clearCardNoErr(){
            this.cardNoErr = '';
        },
        selectMoney(obj){
            this.defaultSeledMoneyVal = obj.value;
            this.getUsc();
        },
        selectYY(obj){
            this.defaultSeledYYVal = obj.value;
        }
    }
});